package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.widget.BlurringView;
import sz.szsmk.citizencard.widget.CommonMethod;

/**
 * author : hyt
 * date : 2019/11/19
 * description : 市民卡页面 viewpager
 */
public class CitizenCardvpAdatper extends PagerAdapter {

    private Context mContext;

    private TextView homeCardName;
    private TextView homeCardBalance;
    private TextView homeCardId;
    private RecyclerView cardViewRv;
    private ImageView iv_bg;
    private ImageView iv_moneyLabel;

    private CheckBox eyrCheck;
    private TextView card_view_user;

    private View homeBg;

    private List<View> viewList;

    private String errmsg;

    private ChangeNickNameClickListener changeNickNameClickListener;

    public CitizenCardvpAdatper(Context mContext,View homeBg,String errmsg){
        this.mContext = mContext;
        this.homeBg = homeBg;
        this.errmsg = errmsg;
        viewList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return  this.dataBeanList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewList.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view;
        if (viewList.size() == position) {
            view = LayoutInflater.from(mContext).inflate(R.layout.card_item_view, null, false);
            viewList.add(view);
        } else {
            view = viewList.get(position);
        }
        view.setTag(position);

        homeCardName = (TextView) view.findViewById(R.id.home_card_name);
        homeCardBalance = (TextView) view.findViewById(R.id.home_card_balance);
        homeCardId = (TextView) view.findViewById(R.id.home_card_id);
        card_view_user = (TextView) view.findViewById(R.id.card_view_user);
        cardViewRv = (RecyclerView) view.findViewById(R.id.card_view_rv);
        eyrCheck = (CheckBox) view.findViewById(R.id.card_view_eye);
        iv_bg = view.findViewById(R.id.iv_bg);
        iv_moneyLabel = view.findViewById(R.id.iv_moneyLabel);

        QueryUserCardRes.DataBean userCardBean = this.dataBeanList.get(position);

        List<QueryUserCardRes.DataBean.CardDetailInfolistBean> cardDetailInfolistBeanList = userCardBean.getCardDetailInfolist();
        if(cardDetailInfolistBeanList.isEmpty()){
            return view;
        }
        QueryUserCardRes.DataBean.CardDetailInfolistBean bean = cardDetailInfolistBeanList.get(0);

        homeCardName.setText(bean.getCardTypeName());





        if (bean != null) {
            double balance = (double) userCardBean.getCardTotalBalance();
            double mBalance = balance / 100;
            String balancestr = doubleToString(mBalance);
            homeCardBalance.setTextSize(30);
            if (userCardBean.getWarningMsg().contains("半年")){
                homeCardBalance.setText(userCardBean.getWarningMsg());
            }else{
                homeCardBalance.setText(eyrCheck.isChecked() ? "* * * * * *" : balancestr);
            }

        }

        String cardNo = userCardBean.getBindCardNo();
        StringBuilder str = new StringBuilder(cardNo.replace(" ", ""));
        int i = str.length() / 4;
        int j = str.length() % 4;
        for (int x = (j == 0 ? i - 1 : i); x > 0; x--) {
            str = str.insert(x * 4, " ");
        }
        homeCardId.setText(str);


        if (bean.getCustName() == null || bean.getCustName().equals("")) {
            card_view_user.setText(bean.getCardNickName());
        } else {
            card_view_user.setText(getUserInfo(bean));
        }

        card_view_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (changeNickNameClickListener != null) {
                    changeNickNameClickListener.onCardNickNameClick(position, userCardBean);
                }
            }
        });


        ArrayList<String> openList  = new ArrayList<>();
        for (int m = 0; m < cardDetailInfolistBeanList.size(); m++) {
            String stateName = cardDetailInfolistBeanList.get(m).getStateName();
            if (stateName != null) {
                if (stateName.equals("已开通")) {
                    //获取已开通的功能卡名称
                    String funcName = cardDetailInfolistBeanList.get(m).getFuncName();
                    openList.add(funcName);
                }
            }
        }

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        cardViewRv.setLayoutManager(manager);
        CardViewFunAdapter funAdapter = new CardViewFunAdapter(DaggerApplication.getIns(), openList);
        cardViewRv.setAdapter(funAdapter);

        eyrCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                double balance = (double) userCardBean.getCardTotalBalance();
                double mBalances = balance / 100;
                String balancestr = doubleToString(mBalances);
                //Toast.makeText(mContext, "点击"+userCardBean.getWarningMsg(), Toast.LENGTH_SHORT).show();

                if (balancestr.equals("")) {
                    ((TextView) viewList.get(position).findViewById(R.id.home_card_balance))
                            .setText(isChecked ? "* * * * * *" : balancestr);
                } else {
                    TextView mBalance = (TextView) viewList.get(position).findViewById(R.id.home_card_balance);

                    mBalance.setText(isChecked ? "* * * * * *" : balancestr);

                }
            }
        });
        container.addView(view);
        return view;
    }

    //显示的数据
    List<QueryUserCardRes.DataBean> dataBeanList = new ArrayList<>();
    public void setData(List<QueryUserCardRes.DataBean> data) {
        this.dataBeanList.clear();
        this.dataBeanList.addAll(data);
        notifyDataSetChanged();
    }


    public static String doubleToString(double num) {
        //使用0.00不足位补0，#.##仅保留有效位
        return new DecimalFormat("0.00").format(num);
    }

    private String getUserInfo(QueryUserCardRes.DataBean.CardDetailInfolistBean dataBean) {
        StringBuilder userInfoStringBuilder = new StringBuilder();
        userInfoStringBuilder.append(dataBean.getCardNickName());
        if (dataBean.getCustName() != null || dataBean.getCustName().equals("")) {
            userInfoStringBuilder.append(" (");
            userInfoStringBuilder.append("持卡人 ");
            userInfoStringBuilder.append(dataBean.getCustName());
            userInfoStringBuilder.append(")");
        }
        return userInfoStringBuilder.toString();
    }

    public void removeItem(int currentPosition) {
        this.dataBeanList.remove(currentPosition);
        notifyDataSetChanged();
    }

    //修改昵称回调
    public interface ChangeNickNameClickListener {
        void onCardNickNameClick(int position, QueryUserCardRes.DataBean dataBean);
    }

    public void setChangeNickNameClickListener(ChangeNickNameClickListener changeNickNameClickListener) {
        this.changeNickNameClickListener = changeNickNameClickListener;
    }

    //修改后昵称设置
    public void refreshCardView(int position, String dataBean) {
        this.dataBeanList.get(position).getCardDetailInfolist().get(0).setCustName(dataBean);
        notifyDataSetChanged();
    }
}
